// Package afos provide an afero OS FS access layer
package afos

import (
	"errors"

	"github.com/spf13/afero"

	"github.com/fclairamb/ftpserver/config/confpar"
	"github.com/fclairamb/ftpserver/fs/utils"
)

// ErrMissingBasePath is triggered when the basePath property isn't specified
var ErrMissingBasePath = errors.New("basePath must be specified")

// LoadFs loads a file system from an access description
func LoadFs(access *confpar.Access) (afero.Fs, error) {

	basePath := access.Params["basePath"]
	if basePath == "" {
		return nil, ErrMissingBasePath
	}
	xinkePath := access.Params["xinkeBasePath"]
	/*if xinkePath == "" {
		return nil, ErrMissingBasePath
	}*/

	basePath = utils.ReplaceEnvVars(basePath)
	xinkePath = utils.ReplaceEnvVars(xinkePath)
	/*if basePath != "" {
		info, err := os.Stat(basePath)
		if err != nil {
			// 如果目录不存在，创建目录
			if os.IsNotExist(err) {
				// 使用exec包的Command来创建目录
				cmd := exec.Command("mkdir", "-p", dirPath)
				err := cmd.Run()
				if err != nil {
					//fmt.Printf("创建目录 %s 时发生错误: %v\\n", dirPath, err)
					//return
				}
				//fmt.Printf("目录 %s 已创建。\\n", dirPath)
			} else {
				// 如果有其他错误，打印错误信息
				//fmt.Printf("检查目录 %s 时发生错误: %v\\n", dirPath, err)
				//return
			}
		} else {
			// 如果目录已经存在，打印信息
			//fmt.Printf("目录 %s 已存在。\\n", dirPath)
		}
	}*/

	return afero.NewBasePathXinkeFs(afero.NewOsFs(), basePath, xinkePath), nil

	//return afero.NewBasePathFs(afero.NewOsFs(), basePath), nil
}
